<?php

//防止恶意调用
if (!defined('IN_MOD'))
{
    die('Hacking attempt');
}

// 自定义模块，自定义区块，自定义配置
// 自定义栏目，自定义插件，自定义扩展，自定义单页，自定义分类

// 验证会员是否登录
function getmemberlogin($memberinfo=array(),$url="")
{
	if(empty($memberinfo))
	{
		moduleerror(array("url"=>$url,"message"=>"请先登录会员"));
	}
}

// 对URL传输加密的数据进行生成签名
function getsign($data=array(),$type=0)
{
	global $configinfo;
	
	if($type == 1)
	{
	  foreach ($data as $key => $val)
	  {
          foreach ($val as $k => $v)
		  {
			$duodata[$k."_"."data".$key] = trim($v);
		  }	
      }	
	  $data = $duodata;
	}
	
	if(is_array($data))
	{
	  ksort($data);
	  
	  foreach ( $data as $k => $v ) 
	  { 
		 $o.= "$k=" . trim( $v ). "&" ;
	  }
	  $getdata = substr($o,0,-1);
	  
    }else{
      $getdata = $data;
    }
	
	$sign = base64_encode(md5(strtoupper($getdata."&key=".$configinfo['signpass'])));	
	
	return $sign;
}
// 对URL传输加密的数据进行验签解密
function getsigndata($data=array(),$sign='',$type=0)
{
	global $configinfo;
	
	if($type == 1)
	{
	  foreach ($data as $key => $val)
	  {
          foreach ($val as $k => $v)
		  {
			$duodata[$k."_"."data".$key] = trim($v);
		  }	
      }	
	  $data = $duodata;
	}
	
	if(is_array($data))
	{
	  ksort($data);
	  
      foreach ( $data as $k => $v ) 
	  { 
		 $o.= "$k=" . trim( $v ). "&" ;
	  }
	  $getdata = substr($o,0,-1);
	  
    }else{
      $getdata = $data;
    }
	
	$res = 0;
	
	// 验签通过后才可以使用数据，安全，否则为不安全数据
	if(md5(strtoupper($getdata."&key=".$configinfo['signpass'])) == base64_decode($sign))
	{
		$res = 1;	
	}	
	
	return $res;
}

// 获取当前商品模块参数
function getshopattrparameter($columnid=0,$adminok = 0)
{
	global $configinfo;
	
	if($columnid)
	{
		$moduleclass = getparentclass($configinfo["ecs_article_cat"],$columnid,$configinfo['cat_id'],$configinfo['parent_id']);
	}else{
		$moduleclass = $configinfo['moduleclass'];
	}
	
	// 规格参数排序设置
	$sort = $adminok ? "" : $configinfo['article_list_order']." asc";
	
	$attrdatalist1=getattrdatalist($configinfo['ecs_article'], $configinfo['article_id'], $configinfo['article_cat_id'], $configinfo['shopparamodule'], $moduleclass['class1'][$configinfo['cat_id']], 0, "", $sort);	
	$attrdatalist2=getattrdatalist($configinfo['ecs_article'], $configinfo['article_id'], $configinfo['article_cat_id'], $configinfo['shopparamodule'], $moduleclass['class2'][$configinfo['cat_id']], 0, "", $sort);	
	$attrdatalist3=getattrdatalist($configinfo['ecs_article'], $configinfo['article_id'], $configinfo['article_cat_id'], $configinfo['shopparamodule'], $moduleclass['class3'][$configinfo['cat_id']], 0, "", $sort);	
	
	if($attrdatalist1)
	{
		$attrdatalist = $attrdatalist1;
	}
	if($attrdatalist2)
	{
		$attrdatalist = $attrdatalist2;
	}
	if($attrdatalist3)
	{
		$attrdatalist = $attrdatalist3;
	}
	
	return $attrdatalist;
}

// 获取商品购物参数
function getshopparameter()
{
	global $configinfo,$configlang;
	
	// 当前商品
	$modulewhere=array();
	$modulewhere["tablename"]="fastxcms_portal_post_product";
	$modulewhere["dataid"]=$configinfo['get_article_id'];
	$modulewhere["dataidname"]=$configinfo['article_id'];
	$goodsinfo=modulepara("datainfo",$modulewhere);
	
	// 订购规格参数获取
	$shopdatalist=$configinfo['shopparameter'];
	$parameterinfo = getmoduleparameter($configinfo['get_article_id'], 3);
	
	// 商品参数html
	$shopdataarr = array();
	foreach($shopdatalist as $key=>$val)
	{
		// 商品已选订购规格参数
		$shoppara_checkpara = $parameterinfo['shoppara_checkpara'][$val[$configinfo['article_id']]];
		if($shoppara_checkpara)
		{
			$shopdataarr[$key]['id'] = $val[$configinfo['article_id']];
			$shopdataarr[$key]['title'] = $val[$configinfo['title']];
			// 已选订购参数列表
			$shoppara_checkpara_group = array();
			foreach($shoppara_checkpara as $key2=>$val2)
			{
				$attrlistinfo = datainfo($configinfo['ecs_article'],$val2,$configinfo['article_id']);
				$shoppara_checkpara_group[$val2] = $attrlistinfo[$configinfo['title']];
			}
			$shopdataarr[$key]['attrlist'] = $shoppara_checkpara_group;
		}
	}
	
	// 其它数据
	$otherdata['shopmoduleurl'] = $configinfo['shopmoduleurl'];
	$otherdata['shopcarturl'] = $configinfo['shopcarturl'];
	$otherdata['dataid'] = intval($configinfo['get_article_id']);
	$otherdata['columnid'] = intval($configinfo['columnid']);
	
	// 获取排序后的商品规格参数
	$otherdata['goodsspecinfo'] = getordergoodsspecinfo($parameterinfo['shoppara_goodsparalist'],$shopdatalist);
	
	// 获取商品规格参数优惠促销价格
	foreach($otherdata['goodsspecinfo']['priceson'] as $key=>$val)
	{
		$otherdata['goodsspecinfo']['priceson'][$key] = getgoodsdiscountprice($val,$goodsinfo['id']);
	}
	
	// 获取商品规格参数积分兑换价格
	$integralstatus_name = getdesignatedname("integralstatus");
	if($goodsinfo[$integralstatus_name] == 1)
	{
		$otherdata['integralgoods'] = 1;// 积分商品
	}
	
	$shopparameterhtml = hook_getshopparameterhtml($shopdataarr,$otherdata,$configlang);	
	
	return $shopparameterhtml;
}

// 获取商品活动列表
function getshopactivitylist()
{
	global $configinfo,$configlang;
	
	// 获取商品活动列表
	$activitylist = getgoodsactivitylist($configinfo['get_article_id']);
		
	// 其它数据
	$otherdata = array();
	
	if(ifarray($activitylist) == 1)
	{
		$shopactivityhtml = hook_getshopactivityhtml($activitylist,$otherdata,$configlang);	
	}
	
	return $shopactivityhtml;
}

// 获取排序后的商品规格参数
function getordergoodsspecinfo($shoppara_goodsparalist=array(),$shopdatalist=array())
{
	global $configinfo;
	
	// 排序后的栏目商品规格参数
	$shopdataarr = array();
	foreach($shopdatalist as $keylist=>$vallist)
	{
		$shopdataarr[] = $vallist[$configinfo['article_id']];
	}
	
	// 当前商品规格参数
	$shoppara_goodsparalist_order = array();
	foreach($shoppara_goodsparalist as $specname=>$speclist)
	{
		// 规格参数列表
		foreach($speclist as $speclistids=>$speclistval)
		{
			// 重组规格参数列表组
			$speclistidarr = getstrtoarr('-',$speclistids);
			$speclistarr = array();
			foreach($speclistidarr as $speckey=>$specid)
			{
				$specinfo = datainfo($configinfo['ecs_article'],$specid,$configinfo['article_id']);// 当前参数列表信息
				$goodsparaid = $specinfo['attrid'];// 当前商品参数ID
				$speclistarr[$goodsparaid] = $specid;
			}
				
			// 排序后的栏目商品规格参数
			$speclistidorderarr = array();
			foreach($shopdataarr as $parakey=>$paraid)
			{
				if($speclistarr[$paraid])
				{
					$speclistidorderarr[] = $speclistarr[$paraid];
				}
			}
			
			$speclistidorderids = getarrtostr("-",$speclistidorderarr);
			
			unset($shoppara_goodsparalist[$specname][$speclistids]);// 删除原有数组
			$shoppara_goodsparalist[$specname][$speclistidorderids] = $speclistval;// 新排序后的数组
		}
	}
	
	return $shoppara_goodsparalist;
}

// 获取商品购物车HTML
function getshopcarthtml()
{
	global $configinfo,$configlang;
	
	// 获取购物车总数量
	$cartnumber = getshopcartnumber();
	
	$shopparameterhtml = hook_getshopcarthtml($cartnumber);	
	
	return $shopparameterhtml;
}

// 获取购物车数量
function getshopcartnumber()
{
	global $configmember;
	
	// 获取购物车总数量
	$cartnumber = 0;
	if($configmember)
	{
		$modulewhere=array();
		$modulewhere['tablename'] = "modulepara_cart";
		$modulewhere['arrayclass'] = $configmember['id'];
		$modulewhere['bigclassname'] = "userid";
		$modulewhere['limit'] = "all";
		$modulewhere['order'] = "default";
		$cartlist = configmodule($modulewhere);
		foreach($cartlist as $key=>$val)
		{
			$cartnumber += $val['number'];
		}
	}
	
	return $cartnumber;
}

// 会员操作数据权限
function memberopprivileges($data_member_id=0,$type=1)
{	
	global $configinfo;
	
	// 单数据操作
	if($type == 1)
	{
		// 删除或修改时
		if($configinfo['expmember_uid'] != $data_member_id)
		{
			return 0;
		}	
	}	

	// 批量数据操作
	if($type == 2)
	{
		// 批量删除或修改时
		foreach($data_member_id as $key=>$val)
		{
			if($configinfo['expmember_uid'] != $val)
			{
				return 0;
			}		
		}
	}
	
	return 1;
}

// 会员操作数据权限（只允许操作自己的数据，限制非法操作其它会员的数据）
function memberopdataok($opdataid=0,$type=1,$tablename="",$dataidname="")
{
	global $configinfo;
	
	// 单数据操作会员权限
	if($type == 1)
	{
		$datainfo = datainfo($tablename,$opdataid,$dataidname,'',$configinfo['moduleid']);
		$memberop = memberopprivileges($datainfo['user_id'],1);
		if($memberop == 0)
		{
			return 0;	
		}
	}
	// 批量数据操作会员权限
	if($type == 2)
	{
		$data_memberid_arr = array();
		foreach($opdataid as $key=>$val)
		{
			$dataid = intval($val);
			$datainfo = datainfo($tablename,$dataid,$dataidname,'',$configinfo['moduleid']);
			$data_memberid_arr[] = $datainfo['user_id'];	
		}
		if(count($data_memberid_arr) > 0)
		{
			$memberop = memberopprivileges($data_memberid_arr,2);
			if($memberop == 0)
			{
				return 0;
			}
		}
	}	
	
	return 1;
}
?>